package com.example.hello.config;

import com.example.hello.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebConfig implements WebMvcConfigurer {

    //主要用于实现无业务逻辑跳转，例如主页跳转，
    //简单的请求重定向，错误页跳转等
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       registry.addViewController("/").setViewName("login");
       registry.addViewController("/index.html").setViewName("login");

        registry.addViewController("/main.html").setViewName("main");
    }



   //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("MyMvcConfig.addInterceptors 注册拦截器");
        registry.addInterceptor(new LoginInterceptor())//注册拦截器
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/login", "/main.html", "/user/login",
                        "/css/**", "/images/**", "/js/**", "/fonts/**");//放行登录页，登陆操作，静态资源
    }
}
